//
// Created by shaowenhan on 2020/5/9.
//

#include "ReadInstanceWrapper.h"

const vector<string> & ReadInstanceWrapper::
elementsInLine(const string &line, int &size, const vector<char> &separators){
    string ele;
    size = 0;
    for(auto c: line)
    {
        bool isCharSeparator = false;
        for(auto &separator: separators)
        {
            if(c == separator)
            {
                if(not ele.empty())
                {
                    m_elements[size++] = ele;
                    ele = "";
                }
                isCharSeparator = true;
                break;
            }
        }
        if(not isCharSeparator)
            ele += c;
    }
    if(not ele.empty())
        m_elements[size++] = ele;
    return m_elements;
}

const vector<int> &ReadInstanceWrapper::
integersInLine(const string &line, int &size, const vector<char> &separators)
{
    elementsInLine(line, size, separators);
    for(int i = 0; i < size; i++)
        m_integers[i] = stoi(m_elements[i]);
    return m_integers;
}

const vector<double> &ReadInstanceWrapper::doublesInLine(const string &line, int &size, const vector<char> &separators)
{
    elementsInLine(line, size, separators);
    for(int i = 0; i < size; i++)
        m_doubles[i] = stod(m_elements[i]);
    return m_doubles;
}

bool ReadInstanceWrapper::isEOF() const
{
    return m_infile.eof();
}

string ReadInstanceWrapper::readLine()
{
    string line;
    getline(m_infile, line);
    return line;
}
